<?php
function lihat(){
        $date='';
        if (isset($_POST['date'])){
            $date = $_POST['date'];
        }
        $carrousel = '';
        if (isset($_POST['CARROUSEL'])){
            $carrousel = $_POST['CARROUSEL'];
        }
        $txtout='';
	$txtout.="
		<form name='form' method='post' action=?>
                <div class='search-container'>
                <div class='search-side'>
		<table border='0' align='center' cellpadding='0' cellspacing='0' class='hitam'>
		<tr>
		<td align='center'>
                <label>DATE: </label><input id='datepicker' name='date' type='text' class='form' size='20' value='$date'>";
//        $txtout.="<label>&nbsp;MILL: </label>".comboODBCPDO('MASTERCARROUSEL','CARROUSELID','CARROUSELNAME','CARROUSEL',"",$carrousel)."";

	$txtout.="</td>
		<td>&nbsp;<input type='submit' name='submit' value='GO &raquo;' class='btn-search'></td>
                </tr>
		</table>
                </div>
                </div>
                </form>";
        
        if($date!=''){
        $carrousel = '3';
        $arrD = explode("/", $date);
        $date = $arrD['2']."-".$arrD['1']."-".$arrD['0'];
        
        $arr_res = arr_result($date, $carrousel);
//        $dataDM = array($date,$mill);
        $sqlTime = "SELECT QCTIMESTRING,ITEMABBREVIATION,BIN,CODE FROM QVQCFINISHPRODUCTQUALITYREPORT WHERE QCDATE = '$date' and CARROUSEL = '$carrousel' and TESTRESULTVALUE > '0.00' GROUP BY QCTIMESTRING,ITEMABBREVIATION,BIN,CODE ORDER BY QCTIMESTRING";
        $res = dbresultODBCPDO($sqlTime);
        if(!exist($sqlTime)){
            echo "<script>alert('DATA TIDAK ADA.. !!')</script>";
            echo "<script>window.history.back();</script>";
            exit();
        }
        $time = array();
        $i=1;
        while($rT = $res->fetch(PDO::FETCH_ASSOC)){
            $time['TIME'][$i] = $rT['QCTIMESTRING'];
            $time['BRAND'][$i] = $rT['ITEMABBREVIATION'];
            $time['BIN'][$i] = $rT['BIN'];
            $time['PRODUCTCODE'][$i] = $rT['CODE'];
            ++$i;
        }
        $jml_code = count($time['PRODUCTCODE']);
        $height_row2=18;
        $width1 = 150;
        $width2 = $width1-($jml_code+7);
        $txtout.="<table id='freeze-table' border='0' bgcolor='#0006C4' class='tabel1'>
                <thead>
                <tr>
                <th colspan='3' class='flexible'><div align='center'>&nbsp;</div></th>
		<th class='tbl-qc-head flexible'><div align='center'>TIME</div></th>";
        foreach ($time['TIME'] as $valueT) {
            $txtout.="<th width='$width1' class='tbl-ijo-muda'><div align='center'>$valueT</div></th>";
        }
        $txtout.="</tr>
                <tr>
                <th colspan='3' class='flexible'><div align='center'>&nbsp;</div></th>
		<th class='tbl-qc-head flexible'><div align='center'>BRAND</div></th>";
        foreach ($time['BRAND'] as $valueB) {
            $txtout.="<th width='$width1' class='wrapword tbl-biru-muda'><div align='center'>$valueB</div></th>";
        }
        $txtout.="</tr>
                <tr>
                <th colspan='3' class='flexible'><div align='center'>&nbsp;</div></th>
		<th class='tbl-qc-head flexible'><div align='center'>FLOUR BIN</div></th>";
        foreach ($time['BIN'] as $valueB) {
            $txtout.="<th width='$width1' class='hrf-putih'><div align='center'>$valueB</div></th>";
        }
        $txtout.="</tr>
                <tr>
                <th colspan='3' class='flexible'><div align='center'>&nbsp;</div></th>
		<th class='tbl-qc-head flexible'><div align='center'>PRODUCT CODE</div></th>";
        foreach ($time['PRODUCTCODE'] as $valueP) {
            $txtout.="<th width='$width1' class='tbl-ijo-muda'><div align='center'>$valueP</div></th>";
        }
        $txtout.="</tr>";
        $txtout.="<tr>
		<th class='tbl-qc-head flexible' width='80'><div align='center'>GROUP</div></th>
                <th class='tbl-qc-head flexible' width='150'><div align='center'>DESCRIPTION</div></th>
                <th class='tbl-qc-head flexible' width='100'><div align='center'>STANDART</div></th>
                <th class='tbl-qc-head flexible' width='100'><div align='center'>UM</div></th>
                <th class='tbl-qc-head flexible' colspan='$jml_code'><div align='center'>RESULT</div></th>";
        $txtout.="</tr>"
                . "</thead>"
                . "<tbody>";
        
        $sqlType = "select QCTYPEID from QVQCFINISHPRODUCTQUALITYREPORT where QCDATE = '$date' and CARROUSEL = '$carrousel' and TESTRESULTVALUE > '0.00' GROUP BY QCTYPEID ORDER BY QCTYPEID";
        $resType = dbresultODBCPDO($sqlType);
        $satu=1;
        while($rType = $resType->fetch(PDO::FETCH_ASSOC)){
            $typeid = $rType['QCTYPEID'];
//            $dataDMT = array($date,$mill,$typeid);
            $sqlTSU = "select TESTID,MIN_MAX,TESTUNITID from QVQCFINISHPRODUCTQUALITYREPORT where QCDATE = '$date' and CARROUSEL = '$carrousel' and QCTYPEID='$typeid' and TESTRESULTVALUE > '0.00' GROUP BY TESTID,MIN_MAX,TESTUNITID ORDER BY TESTID";
//            echo $sqlTSU."<br>";
            $resTSU= dbresultODBCPDO($sqlTSU);
            $x = 1;
            $xxx = array();
            while($rTSU = $resTSU->fetch(PDO::FETCH_ASSOC)){
                $xxx['DESCRIPTION'][$x] = $rTSU['TESTID'];
                $xxx['STANDART'][$x] = $rTSU['MIN_MAX'];
                $xxx['UM'][$x] = $rTSU['TESTUNITID'];
                $xxx[$rTSU['TESTID']][$rTSU['MIN_MAX']][$x]=$rTSU['TESTUNITID'];
                ++$x;
            }
            
            $jml_standart = count($xxx['STANDART']);
            $txtout.="<tr>
                    <td class='tbl-abu flexible' width='80'><div align='center'>".$typeid."</div></td>";
            $txtout.="<td class='tbl-abu flexible' width='150'>
                    <table width='100%' border='0' bgcolor='#FFFFFF' class='tabel1 flexible'>";
            $temp1="";
            foreach ($xxx['DESCRIPTION'] as $valueD){
                if ($temp1 != $valueD){
                    $jml_rowUM=0;
                    foreach ($xxx[$valueD] as $key => $valueST){
                            $x = count($xxx[$valueD][$key]);
                            $jml_rowUM = $jml_rowUM+$x;
                    }
                if ($jml_rowUM==1){
                    $height = ($jml_rowUM*$height_row2);
                }else{
                    $height = ($jml_rowUM*$height_row2)+(($jml_rowUM-1)*2);
                }
                $txtout.="<tr height='$height'>
                          <td class='tbl-abu text-left'>".$valueD."</td>
                          </tr>
                          ";
                }
                $temp1 = $valueD;
            }
            $txtout.="</table>
                      </td>";
            
            $txtout.="<td class='tbl-abu flexible' width='100'>
                    <table width='100%' border='0' bgcolor='#FFFFFF' class='tabel1'>";
            foreach ($xxx['STANDART'] as $valueS){
            $txtout.="<tr height='$height_row2'>
                      <td class='tbl-abu flexible'><div align='center'>".$valueS."</div></td>
                      </tr>
                      ";
            }
            $txtout.="</table>
                      </td>";
                
            $txtout.="<td class='tbl-abu flexible' width='100'>
                    <table width='100%' border='0' bgcolor='#FFFFFF' class='tabel1'>";
            foreach ($xxx['UM'] as $valueU){
            $txtout.="<tr height='$height_row2'>
                      <td class='tbl-abu flexible'><div align='center'>".$valueU."</div></td>
                      </tr>
                      ";
            }
            $txtout.="</table>
                      </td>";
                    
            
            $txtout.="<td colspan='$jml_code' class='tbl-abu flexible'>
                    <table width='100%' border='0' bgcolor='#FFFFFF' class='tabel1'>";
            for ($a=1;$a<=$jml_standart;$a++){
                        $txtout.="<tr height='$height_row2'>";
                        for ($b=1;$b<=$jml_code;$b++){
                            $pcode = $time['PRODUCTCODE'][$b];
                            $timestring = $time['TIME'][$b];
                            $desc = $xxx['DESCRIPTION'][$a];
                            $standart = $xxx['STANDART'][$a];
                            $testResult = $arr_res[$desc][$standart][$timestring][$pcode];
                            $txtout.="<td class='tbl-abu text-left' width='$width2'><div align='center'>".$testResult."</div></td>";
                        }
                        $txtout.="</tr>";
            }
            $txtout.="</table>
                      </td>";
            
            $txtout.="</tr>";
            $satu++;
        }
        $txtout.="</tbody>"
                . "</table>";
        }
	return $txtout;
}

function arr_result($date,$carrousel){
//            $dataDM = array($date,$mill);
            $sql = "select TESTID,MIN_MAX,QCTIMESTRING,CODE,TESTRESULTVALUE from QVQCFINISHPRODUCTQUALITYREPORT where QCDATE='$date' and CARROUSEL='$carrousel' and TESTRESULTVALUE > '0.00'";
            $res= dbresultODBCPDO($sql);
            $arr_temp = array();
            while($rs = $res->fetch(PDO::FETCH_ASSOC)){
                $arr_temp[$rs['TESTID']][$rs['MIN_MAX']][$rs['QCTIMESTRING']][$rs['CODE']] = $rs['TESTRESULTVALUE'];
            }
//        echo "<pre>";
//	print_r($arr_temp);
//	echo "</pre>"; 
	return $arr_temp;

}

function generate_form(){
	switch (isset($_REQUEST['mode'])) {
		default:
		$txtout=lihat();
		break;
	}
	return $txtout;
}
?>